package com.ptl.bp.config;

import com.ptl.bp.config.enums.UserRole;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import static com.ptl.bp.config.enums.UserRole.*;

@Aspect
@Component
public class PermissionAspect {
    @Before("@annotation(permissionRequire)")
    public void checkPermission(PermissionRequire permissionRequire) {
        GlobalUserContext.CurrentUser user = GlobalUserContext.getUser();
        if (user == null) {
            throw new BizException(ApiResult.ResStatusEnum.FORBIDDEN,"没有权限");
        }
        UserRole role = permissionRequire.value();
        boolean hasPermission = false;
        switch (role) {
            case ADMIN:
                if (user.getRole() == ADMIN) {
                    hasPermission = true;
                }
                break;
            case RIGHT:
                if (user.getRole() == ADMIN || user.getRole() == RIGHT) {
                    hasPermission = true;
                }
                break;
        }
        if (!hasPermission) {
            throw new BizException("需要内容管理员权限");
        }
    }
}